<?php
namespace app\repair\model;
use app\common\model\Common;
use think\Request;
use think\Db;
class Fault extends Common
{
	protected $append = ['type_text'];
	protected $export_field = [];//导入数据字段、顺序对应
	protected $ty = 0;//类型
	protected $valite_field = ['describes'];//唯一字段

	/**
	* 导入数据
	*/
	public function exportData($data, $ty, $type = '')
	{
		$this->exportField($ty);
		foreach($data as $k => $v){
			$arr = [];
			foreach($this->export_field as $key => $val){
				$arr[$val] = str_replace(["\r","\n"], '', $v[$key]);
				$arr['type'] = $this->ty;
			}
			$res = $this->isUpdate(false)->validate('Fault.add')->save($arr);
			if($res === false){
				return false;
			}
		}
	}
	/**
	* 导入数据字段处理
	*/
	protected function exportField($ty)
	{
		if($ty == 'cate'){
			$this->export_field = ['fid','describes'];
			$this->ty = 0;
		}else if($ty == 'fault'){
			$this->export_field = ['describes'];
			$this->ty = 1;
		}else if($ty == 'outward'){
			$this->export_field = ['describes'];
			$this->ty = 2;
		}
	}
	/**
	 * 获取分类信息
	 */
	public function listData($where, $order, $page, $row, $search = ''){
		if( $row == 'limit' ){
			
			if($where['type'] == 0){
				//分类
				return $this->allList(0, $where, $order, $search);
			}else{
				//不是分类
				return $this->where($search)->where($where)->order($order)->select();
			}
			
		}else{
			return $this->where($search)->where($where)->order($order)->page($page, $row)->select();
		}
	}
	/**
	 * 获取分类列表
	 * 分类+品牌
	 */
	public function getCate($fid, $num = -1)
	{
		global $data;
		$info = $this->sun_cate($fid);
		if($info){
			$num++;
			foreach($info as $k => $v){
				$v['describes'] = str_repeat('--',$num).$v['describes'];
				$data[] = $v;
				if($num <= 0){
					if($this->sun_cate($v['fault_id'])){
						$this->getCate($v['fault_id'],$num);
					}
				}
			}
		}
		return $data;
	}
	/**
	 * 删除分类及子分类
	 */
	public function del($ids)
	{
		$this->where('fault_id','in',$ids)->delete();
		$sun = $this->where('fid','in',$ids)->select();
		if($sun){
			foreach($sun as $k => $v){
				return $this->del($v['fault_id']);
			}
		}
		return true;
	}
	/**
	 * 获取器
	 */
	public function getTypeTextAttr($value, $data)
	{
		switch($data['type']){
			case 0: return '设备分类';break;
			case 1: return '故障描述';break;
			case 2: return '外观描述';break;
			case 3: return '设备品牌';break;
			case 4: return '设备型号';break;
			default: return '异常';break;
		}
	}


	/************************************** 私有 ****************************************/
	/**
	 * 获取子级分类
	 */
	private function sun_cate($fid)
	{
		return $this->where('fid',$fid)->where('type',0)->order('fault_id desc')->select();
	}
	/**
	 * 获取分类、品牌、型号集
	 * @param intger $fid 父级id
	 */
	private function allList($fid, $where, $order, $search, $info = [], $num = 0)
	{
		global $data;
		if(!$info){
			$info = $this->where('fid',$fid)->where($where)->where($search)->order($order)->select();
		}
		if($info){
			$num++;
			foreach($info as $val){
				$val->describes = str_repeat('&nbsp;', 2*$num) . $val->describes;
				$val->children = false;
				$val->level = $num;
				$data[] = $val;
				$sun = $this->where('fid',$val['fault_id'])->where($search)->order($order)->select();
				if($sun){
					$val->children = true;
					$this->allList($val['fault_id'], $where, $order, $search, $sun, $num);
				}
			}
		}
    	return $data;
	}
}